package com.BMS.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.BMS.Service.BookService;
import com.BMS.Service.UserService;
import com.BMS.Service.ViolationService;
import com.BMS.entity.Book;
import com.BMS.entity.Borrow;
import com.BMS.entity.Reader;
import com.BMS.entity.Violation;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 违规记录实体类控制层接口
 * @author lenovo
 *
 */
@Controller
@RequestMapping("/Violation")
public class ViolationController {

	private ViolationService violationService;
	private BookService bookService;
	private UserService userService;
	
	@Autowired
	public void setViolationService(ViolationService violationService) {
		this.violationService = violationService;
	}
	
	@Autowired
	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	/*
	 * 违规记录数据渲染接口
	 * url:"/Violation/showViolation.do"
	 * 正确：返回code="200",violationList=violationList,pageCount=pageCount
	 * 错误：返回code="401",msg="暂无违规记录"
	 *     返回code="402",msg="当前页码错误",violationList=violationList,nowPage=1,pageCount=pageCount
	 *     返回code="403",msg="获取数据失败"
	 */
	@ResponseBody
	@RequestMapping(value="/showViolation.do",method=RequestMethod.GET,produces={"application/json;charset=UTF-8"})
	public String ShowViolation(HttpServletRequest request, HttpServletResponse response) {
		// 初始化json对象
		JSONObject obj = new JSONObject();
		
		// 获取参数
		// 排序字段
		String orderBy = request.getParameter("orderBy");
		// 当前页码
		int nowPage = Integer.parseInt(request.getParameter("nowPage"));
		
		// 初始化数据列表
		List<Violation> violationList = null;
		
		// 获取总页码
		long pageCount = violationService.getPageCount();
		System.out.println(pageCount);
		if(pageCount==0) {
			// 暂无数据
			obj.put("code", "401");
			obj.put("msg", "暂无违规记录！");
			obj.put("pageCount", 0);
			return obj.toString();
		} else {
			// 有数据
			if(nowPage>pageCount) {
				// 当前页码错误
				violationList = violationService.findAllViolations(0, 10, orderBy);
				obj.put("violationList", JSON.toJSONString(violationList,true));
				obj.put("pageCount", JSON.toJSONString(pageCount,true));
				obj.put("nowPage", JSON.toJSONString(1,true));
				obj.put("code", "402");
				obj.put("msg", "当前页码错误！");
				return obj.toString();
			} else {
				// 页码正确
				violationList = violationService.findAllViolations(10*(nowPage-1), 10, orderBy);
				obj.put("violationList", JSON.toJSONString(violationList,true));
				obj.put("pageCount", JSON.toJSONString(pageCount,true));
				obj.put("code", "200");
				return obj.toString();
			}
		}
	}
	
	/*
	 * 违规记录更新控制层接口
	 * 接受前端ajax post请求
	 * 返回json格式数据
	 * 正确：返回code="200"
	 * 错误：返回code="401" - 读者ID不存在
	 *     返回code="402" - 图书ID不存在
	 *     返回
	 */
	@ResponseBody
	@RequestMapping(value="/violationUpdate.do",method=RequestMethod.POST,produces={"application/json;charset=UTF-8"})
	public String violationUpdate(Violation violation) {
		// 初始化JSON对象
		JSONObject obj = new JSONObject();
		
		// 检查读者ID
		String reader_id = violation.getReader_ID();
		Reader reader = userService.findReaderById(reader_id);
		if(reader==null) {
			obj.put("code", "401");
			obj.put("msg", "读者ID不存在！");
			return obj.toString();
		}
		
		// 检查图书
		String book_id = violation.getBook_ID();
		Book book = bookService.findBookById(book_id);
		if(book==null) {
			obj.put("code", "402");
			obj.put("msg","图书ID不存在！");
			return obj.toString();
		}
		
		// 更新违规记录
		violationService.updateViolation(violation);
		obj.put("code", "200");
		obj.put("msg", "更新成功！");
		return obj.toString();
		
	}
	
	/*
	 * 违规记录删除功能控制层接口
	 * 接受前端ajax POST请求
	 * 返回json格式数据
	 * 正确：返回code="200",msg="删除成功"
	 * 错误：返回code="401",msg="违规记录不存在"
	 */
	@ResponseBody
	@RequestMapping(value="/violationDel.do",method=RequestMethod.POST,produces={"appliction/json;charset=UTF-8"})
	public String violationDel(int violation_id) {
		// 初始化JSON对象
		JSONObject obj = new JSONObject();
		
		// 查找该违规记录
		Violation violation = violationService.findViolationById(violation_id);
		if(violation==null) {
			obj.put("code","401");
			obj.put("msg", "该违规记录不存在！");
			return obj.toString();
		} else {
			violationService.deleteViolationById(violation_id);
			obj.put("code", "200");
			obj.put("msg","删除成功！");
			return obj.toString();
		}
	}
	
	/*
	 * 违规登记控制层接口
	 * 接受前端ajax POST请求
	 * 返回json格式数据
	 * 正确：返回code="200"
	 * 错误：返回code="401" - 读者ID不存在
	 *     返回code="402" - 图书ID不存在
	 *     返回code="500" - 操作失败
	 *     
	 */
	@ResponseBody
	@RequestMapping(value="/violationReg.do",method=RequestMethod.POST,produces={"application/json;charset=UTF-8"})
	public String BorrowBook(HttpServletRequest request, HttpServletResponse response) {
		// 生成json对象
		JSONObject obj = new JSONObject();
		// 获取输入读者ID
		String reader_id = request.getParameter("reader_id");
		// 检查读者ID是否存在
		Reader reader = userService.findReaderById(reader_id);
		if(reader==null) {
			// 读者ID不存在
			obj.put("code", "401");
			obj.put("msg", "读者ID不存在!");
			return obj.toString();
		}
		// 获取输入图书ID
		String book_id = request.getParameter("book_id");
		// 检查图书ID是否存在
		Book book = bookService.findBookById(book_id);
		if(book==null) {
			// 图书ID不存在
			obj.put("code", "402");
			obj.put("msg", "图书ID不存在！");
			return obj.toString();
		} else {
			try {
				// 获取违规类型
				String type = request.getParameter("type");
				// 获取管理员ID
				String manager_id = request.getParameter("manager_id");
				// 获取登记日期
				String date = request.getParameter("reg_date");
				
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/M/d");
				Date reg_date = sdf1.parse(date);
				
				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
				reg_date = sdf2.parse(sdf2.format(reg_date));
				
				// 获取当前状态
				boolean status = Boolean.valueOf(request.getParameter("status"));
				
				// 封装borrow对象
				Violation violation = new Violation();
				violation.setType(type);
				violation.setReader_ID(reader_id);
				violation.setManager_ID(manager_id);
				violation.setBook_ID(book_id);
				violation.setReg_date(reg_date);
				violation.setStatus(status);
				// 保存borrow实例
				violationService.addViolation(violation);
				// 成功保存
				obj.put("code", "200");
				obj.put("msg", "登记成功！");
				return obj.toString();
			} catch (Exception e) {
				obj.put("code", "500");
				obj.put("msg", "操作失败！");
				e.printStackTrace();
				return obj.toString();
			}
		}
		
	}
}
